package com.service.impl;

import com.dao.RoomDao;
import com.pojo.Room;
import com.pojo.RoomType;
import com.service.RoomService;
import com.util.ConstUtils;
import com.util.JedisPoolUtils;
import com.util.RedisKey;
import com.vo.RoomVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.List;

/**
 * @author mornd
 * @date 2020/11/26 - 11:10
 */
@Service
@Transactional
public class RoomServiceImpl implements RoomService {
    @Autowired
    private RoomDao roomDao;

    /**
     * 根据条件查询房间
     * @param roomVo
     * @return
     */
    public List<Room> findRoomList(RoomVo roomVo) {
        return roomDao.findRoomList(roomVo);
    }

    /**
     * 添加房间
     * @param room
     * @return
     */
    @Override
    public int addRoom(Room room) {
        return roomDao.addRoom(room);
    }

    /**
     * 修改房间
     * @param room
     * @return
     */
    public int updateRoom(Room room) {
        //根据id获取房型详情
        Room roomById = findRoomById(room.getId());
        //如果数据库中的图片地址不等于上传的地址 则说明用户修改了图片 则删除之前的图片
        if(!roomById.getPhoto().equals(room.getPhoto())){
            File updateImage = new File(ConstUtils.ROOM_MAIN_UPLOAD_PATH + roomById.getPhoto());
            if(updateImage.exists()){
                //父文件夹对象
                File delParent = new File(String.valueOf(updateImage.getParentFile()));
                //删除老图片
                updateImage.delete();
                //删除图片后 如果该图片所属的文件夹为空 则删除该文件夹
                if(delParent.list().length == 0){
                    delParent.delete();
                }
            }
        }
        return roomDao.updateRoom(room);
    }

    /**
     * 删除房间
     * @param roomId
     * @param deleteImagePath
     * @return
     */
    public boolean deleteRoom(Integer roomId,String deleteImagePath) {
        try {
            //如果不是默认图片则删除
            if(!ConstUtils.ROOMTYPE_DEFAULT_IMAGE_NAME.equals(deleteImagePath)){
                //删除的图片对象
                File delImage = new File(ConstUtils.ROOM_MAIN_UPLOAD_PATH + deleteImagePath);
                //删除该房型对应的图片
                if(delImage.exists()){
                    //父文件夹对象
                    File delParent = new File(String.valueOf(delImage.getParentFile()));
                    delImage.delete();
                    //删除图片后 如果该图片所属的文件夹为空 则删除该文件夹
                    if(delParent.list().length == 0){
                        delParent.delete();
                    }
                }
            }
            //删除数据库数据
            roomDao.deleteRoom(roomId);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 根据房间id查询房间详情
     * @param roomId
     * @return
     */
    @Override
    public Room findRoomById(Integer roomId) {
        return roomDao.findRoomById(roomId);
    }

    /**
     * 根据room的id查询room、roomType、floor 前台
     *
     * @param id
     * @return
     */
    @Override
    public Room findRoomAndRoomTypeAndFloorById(Integer id) {
        return roomDao.findRoomAndRoomTypeAndFloorById(id);
    }
}
